<template>

</template>

<script setup lang="ts">
import {onMounted, onUnmounted, ref} from "vue";

let emits = defineEmits(['message']);
const webSocket = ref()
const flag = ref(false)
let Pone: number
onMounted(() => {
    webSocket.value = new WebSocket("ws://127.0.0.1:2024/public/ws");
    webSocket.value.onopen = function () {
        console.log("连接成功！");
        Pone = setInterval(() => {
            if (flag.value) {
                webSocket.value.send("pone..")
            }
        }, 2000)
    };
    webSocket.value.onmessage = function (evt) {
        let message: string = evt.data;
        emits('message', message)
    };
    webSocket.value.onclose = function () {
        flag.value = false
        clearInterval(Pone)
        console.log("close...")
    };
    webSocket.value.onerror = function () {
        flag.value = false
        clearInterval(Pone)
        console.log("error..！");
    };
})
onUnmounted(() => {

})
</script>

<style scoped>

</style>